package persistencia;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import bean.MateriaVO;

public class MateriaDAO {

	private static MateriaDAO ins;
	
	private MateriaDAO(){
		
	}
	
	public static MateriaDAO getInstancia(){
		
		if(ins == null)
			ins = new MateriaDAO();
		return ins;
		
	}
	
	public MateriaVO obtener(MateriaVO arg){
		
		MateriaVO m = null;
		String sql = "Select * from Materia Where numero = ?";
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			Connection con = DataSource.getConexion();
			stmt = con.prepareStatement(sql);
			stmt.setString(1, arg.getNumero());
			rs = stmt.executeQuery();
			if(rs.next()){
				m = new MateriaVO();
				m.setNumero(rs.getString(1));
				m.setNombre(rs.getString(2));
			}
			DataSource.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
		
	}
	
	
	public Vector<MateriaVO> obtenerTodos(){
		
        Vector<MateriaVO> materias = new Vector<MateriaVO>();
		String sql = "Select * from Materia";
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			Connection con = DataSource.getConexion();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				MateriaVO m = new MateriaVO();
				m.setNumero(rs.getString(1));
				m.setNombre(rs.getString(2));
				materias.add(m);
			}
			DataSource.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return materias;
		
	}
	
	
	public void guardar(MateriaVO m){
		
		String sql = "Insert into Materia values (?,?)";
		PreparedStatement stmt = null;
		
		try {
			Connection con = DataSource.getConexion();
			stmt = con.prepareStatement(sql);
			stmt.setString(1, m.getNumero());
			stmt.setString(2, m.getNombre());
			stmt.execute();
			DataSource.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	
	public void actualizar(MateriaVO m){
		
		String sql = "Update Materia Set nombre = ? Where numero =? ";
		PreparedStatement stmt = null;
		
		try {
			Connection con = DataSource.getConexion();
			stmt = con.prepareStatement(sql);
			stmt.setString(2, m.getNumero());
			stmt.setString(1, m.getNombre());
			stmt.execute();
			DataSource.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public void eliminar(MateriaVO arg){
	
		String sql = "Delete Materia Where numero = ? ";
		PreparedStatement stmt = null;
		
		try {
			Connection con = DataSource.getConexion();
			stmt = con.prepareStatement(sql);
			stmt.setString(1, arg.getNumero());
			stmt.execute();
			DataSource.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	
	
	
}
